home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / 4dos / 4utilsf.zip / HANDLEIN.PAS < prev    next >
Pascal/Delphi Source File  |  1992-12-10  |  6KB  |  201 lines

  1. UNIT HandleIniFile;
  2. {$D-}
  3. (* ----------------------------------------------------------------------
  4.    Part of 4DESC - A Simple 4DOS File Description Editor
  5.        and 4FF   - 4DOS File Finder
  6.  
  7.    (c) 1992 Copyright by David Frey,
  8.                          Urdorferstrasse 30
  9.                          8952 Schlieren ZH
  10.                          Switzerland
  11.  
  12.        Code created using Turbo Pascal 6.0 (c) Borland International 1990
  13.  
  14.    DISCLAIMER: This unit is freeware: you are allowed to use, copy
  15.                and change it free of charge, but you may not sell or hire
  16.                this part of 4DESC. The copyright remains in our hands.
  17.  
  18.                If you make any (considerable) changes to the source code,
  19.                please let us know. (send a copy or a listing).
  20.                We would like to see what you have done.
  21.  
  22.                We, David Frey and Tom Bowden, the authors, provide absolutely
  23.                no warranty of any kind. The user of this software takes the
  24.                entire risk of damages, failures, data losses or other
  25.                incidents.
  26.  
  27.    This unit handles the reading of settings stored in 4TOOLS.INI.
  28.  
  29.    ----------------------------------------------------------------------- *)
  30.  
  31. INTERFACE
  32.  
  33. CONST INIFileExists : BOOLEAN = FALSE;
  34.  
  35. PROCEDURE ReadIniFile;
  36.  
  37. FUNCTION  ReadSettingsChar(Section,Name: STRING; Default: CHAR): CHAR;
  38. FUNCTION  ReadSettingsString(Section,Name: STRING; Default: STRING): STRING;
  39. FUNCTION  ReadSettingsInt(Section,Name: STRING; Default: INTEGER): INTEGER;
  40. FUNCTION  ReadSettingsColor(Section,Name: STRING; Default: INTEGER): BYTE;
  41.  
  42. IMPLEMENTATION USES Dos, StringDateHandling;
  43.  
  44. CONST INIFileName = '4TOOLS.INI';
  45.       MaxLines    = 32;
  46.  
  47.  
  48. VAR INIFile : TEXT;
  49.     INIPath : DirStr;
  50.     MaxLine : INTEGER;
  51.     Line    : ARRAY[1..MaxLines] OF STRING[48];
  52.  
  53.     Name    : NameStr;
  54.     Ext     : ExtStr;
  55.  
  56. PROCEDURE ReadIniFile;
  57.  
  58. VAR semicol : BYTE;
  59.     ReadLine: STRING;
  60.     s       : STRING;
  61.  
  62. BEGIN
  63.  (* Search strategy for 4TOOLS.INI :
  64.       i) Directory where this application has started from.
  65.      ii) Environment variable 4TOOLS
  66.     iii) Path                                                   *)
  67.  
  68.  FSplit(ParamStr(0),INIPath,Name,Ext);
  69.  IF INIPath[Length(INIPath)] = '\' THEN Delete(INIPath,Length(INIPath),1);
  70.  s := GetEnv('4TOOLS');
  71.  IF s > '' THEN
  72.   BEGIN
  73.    IF s[Length(s)] = '\' THEN Delete(s,Length(s),1);
  74.    INIPath := INIPath + ';'+s;
  75.   END;
  76.  INIPath := INIPath + ';'+GetEnv('PATH');
  77.  IF INIPath[Length(INIPath)] = '\' THEN Delete(INIPath,Length(INIPath),1);
  78.  INIPath := FSearch(INIFileName,INIPath);
  79.  
  80.  INIFileExists := (INIPath > '');
  81.  IF INIFileExists THEN
  82.   BEGIN
  83.    Assign(INIFile,INIPath);
  84.    Reset(INIFile);
  85.    INIFileExists := (IOResult = 0);
  86.  
  87.    IF IniFileExists THEN
  88.     BEGIN
  89.      MaxLine := 1;
  90.      WHILE NOT Eof(INIFile) AND (MaxLine <= MaxLines) DO
  91.       BEGIN
  92.        ReadLn(INIFile,ReadLine);
  93.        StripLeadingSpaces(ReadLine);
  94.  
  95.        IF ReadLine[1] <> ';' THEN
  96.         BEGIN
  97.          semicol := Pos(';',ReadLine);
  98.          IF semicol > 0 THEN ReadLine := Copy(ReadLine,1,semicol-1);
  99.  
  100.          IF Length(ReadLine) > 0 THEN
  101.           BEGIN
  102.            StripTrailingSpaces(ReadLine);
  103.            Line[MaxLine] := DownStr(ReadLine); INC(MaxLine);
  104.           END;
  105.         END; (* IF Line[1] <> ';' .. *)
  106.       END; (* WHILE *)
  107.  
  108.      IF NOT Eof(INIFile) THEN
  109.       BEGIN
  110.        WriteLn('WARNING: '''',INIFileName,'''' exceeding ',MaxLines,' lines!');
  111.        WriteLn('         Parts of your settings will be ignored.');
  112.       END;
  113.  
  114.      Close(INIFile);
  115.     END; (* INIFileExists .. *)
  116.   END; (* INIFileExists .. *)
  117. END; (* ReadIniFile *)
  118.  
  119. FUNCTION  ReadSettingsString(Section,Name: STRING; Default: STRING): STRING;
  120.  
  121. VAR LineNr: BYTE;
  122.     eq    : BYTE;
  123.     s,res : STRING;
  124.  
  125. BEGIN
  126.  LineNr := 1;
  127.  Section := '['+DownStr(Section)+']';
  128.  WHILE (Line[LineNr] <> Section) AND (LineNr <= MaxLine) DO INC(LineNr);
  129.  IF Line[LineNr] = Section THEN
  130.   BEGIN
  131.    DownString(Name); res := ''; s := '';
  132.    REPEAT
  133.     s  := Line[LineNr]; eq := Pos('=',s);
  134.     IF eq > 0 THEN s := Copy(s,1,eq-1);
  135.     StripTrailingSpaces(s);
  136.     INC(LineNr);
  137.    UNTIL (s = Name) OR (LineNr > MaxLine);
  138.  
  139.    IF s = Name THEN
  140.     BEGIN
  141.      res := Copy(Line[LineNr-1],eq+1,255);
  142.      StripLeadingSpaces(res);
  143.     END
  144.    ELSE res := Default;
  145.   END
  146.  ELSE res:= Default;
  147.  ReadSettingsString := res;
  148. END;
  149.  
  150. FUNCTION  ReadSettingsChar(Section,Name: STRING; Default: CHAR): CHAR;
  151.  
  152. VAR s : STRING;
  153.  
  154. BEGIN
  155.  s := ReadSettingsString(Section,Name,Default);
  156.  ReadSettingsChar := s[1];
  157. END;
  158.  
  159. FUNCTION  ReadSettingsInt(Section,Name: STRING; Default: INTEGER): INTEGER;
  160.  
  161. VAR s  : STRING;
  162.     res: INTEGER;
  163.     v  : INTEGER;
  164.  
  165. BEGIN
  166.  Str(Default,s);
  167.  s := ReadSettingsString(Section,Name,s);
  168.  Val(s,v,res);
  169.  IF res > 0 THEN v := Default;
  170.  ReadSettingsInt := v;
  171. END;
  172.  
  173. FUNCTION  ReadSettingsColor(Section,Name: STRING; Default: INTEGER): BYTE;
  174.  
  175. CONST color : ARRAY[0..15] OF STRING[12] =
  176.               ('black'   ,'blue'        ,'green'     ,'cyan'     ,
  177.                'red'     ,'magenta'     ,'brown'     ,'lightgray',
  178.                'darkgray','lightblue'   ,'lightgreen','lightcyan',
  179.                'lightred','lightmagenta','yellow'    ,'white');
  180.  
  181. VAR s  : STRING;
  182.     c  : BYTE;
  183.  
  184. BEGIN
  185.  Str(Default,s);
  186.  s := ReadSettingsString(Section,Name,'');
  187.  
  188.  IF s > '' THEN
  189.   BEGIN
  190.    c := 0;
  191.    WHILE (color[c] <> s) AND (c<16) DO INC(c);
  192.    IF color[c] <> s THEN c := Default;
  193.   END
  194.  ELSE c := Default;
  195.  ReadSettingsColor := c;
  196. END;
  197.  
  198. BEGIN
  199.  ReadIniFile;
  200. END.
  201.